package util

import (
	"bytes"
	"fmt"
	retalog "github.com/lestrrat-go/file-rotatelogs"
	"github.com/rifflock/lfshook"
	"github.com/sirupsen/logrus"
	"path/filepath"
	"time"
)

var logger *logrus.Logger
func BaseLogger() *logrus.Logger {
	filePath :="log/log"
	//src,_ := os.OpenFile(filePath,os.O_RDWR|os.O_CREATE,0755)
	logger = logrus.New()

	//logger.Out =src
	logger.SetLevel(logrus.InfoLevel)
	// 显示行
	logger.SetReportCaller(true)

	logWriter, _ := retalog.New(
		filePath+"%Y%m%d.log",
		retalog.WithMaxAge(7*24*time.Hour),			// 文件最大保存时间
		retalog.WithRotationTime(24*time.Hour),		// 日志切割时间间隔
		)
	writeMap :=lfshook.WriterMap{
		logrus.InfoLevel: logWriter,
		logrus.FatalLevel: logWriter,
		logrus.DebugLevel: logWriter,
		logrus.WarnLevel: logWriter,
		logrus.ErrorLevel: logWriter,
		logrus.PanicLevel: logWriter,
	}
	Hook := lfshook.NewHook(writeMap,&MyFormatter{})
	logger.AddHook(Hook)

	return logger
}

//订单日志
func OrderLogger() *logrus.Logger {
	filePath :="log/data/order"
	//linkName := "latest_log.log"
	//src,_ := os.OpenFile(filePath,os.O_RDWR|os.O_CREATE,0755)
	logger = logrus.New()

	//logger.Out =src
	logger.SetLevel(logrus.InfoLevel)
	// 显示行
	logger.SetReportCaller(true)

	logWriter, _ := retalog.New(
		filePath+"%Y%m%d.log",
		retalog.WithMaxAge(7*24*time.Hour),			// 文件最大保存时间
		retalog.WithRotationTime(24*time.Hour),		// 日志切割时间间隔
		//retalog.WithLinkName(linkName),				// 生成软链，指向最新日志文件
	)
	writeMap :=lfshook.WriterMap{
		logrus.InfoLevel: logWriter,
		logrus.FatalLevel: logWriter,
		logrus.DebugLevel: logWriter,
		logrus.WarnLevel: logWriter,
		logrus.ErrorLevel: logWriter,
		logrus.PanicLevel: logWriter,
	}
	Hook := lfshook.NewHook(writeMap,&MyFormatter{})
	logger.AddHook(Hook)

	return logger
}

//策略日志
func StrategyLogger() *logrus.Logger {
	filePath :="log/data/strategy"
	//linkName := "latest_log.log"
	//src,_ := os.OpenFile(filePath,os.O_RDWR|os.O_CREATE,0755)
	logger = logrus.New()

	//logger.Out =src
	logger.SetLevel(logrus.InfoLevel)
	// 显示行
	logger.SetReportCaller(true)

	logWriter, _ := retalog.New(
		filePath+"%Y%m%d.log",
		retalog.WithMaxAge(7*24*time.Hour),			// 文件最大保存时间
		retalog.WithRotationTime(24*time.Hour),		// 日志切割时间间隔
		//retalog.WithLinkName(linkName),				// 生成软链，指向最新日志文件
	)
	writeMap :=lfshook.WriterMap{
		logrus.InfoLevel: logWriter,
		logrus.FatalLevel: logWriter,
		logrus.DebugLevel: logWriter,
		logrus.WarnLevel: logWriter,
		logrus.ErrorLevel: logWriter,
		logrus.PanicLevel: logWriter,
	}
	Hook := lfshook.NewHook(writeMap,&MyFormatter{})
	logger.AddHook(Hook)

	return logger
}

//model日志
func ModelLogger() *logrus.Logger {
	filePath :="log/data/model"
	//linkName := "latest_log.log"
	//src,_ := os.OpenFile(filePath,os.O_RDWR|os.O_CREATE,0755)
	logger = logrus.New()

	//logger.Out =src
	logger.SetLevel(logrus.InfoLevel)
	// 显示行
	logger.SetReportCaller(true)

	logWriter, _ := retalog.New(
		filePath+"%Y%m%d.log",
		retalog.WithMaxAge(7*24*time.Hour),			// 文件最大保存时间
		retalog.WithRotationTime(24*time.Hour),		// 日志切割时间间隔
		//retalog.WithLinkName(linkName),				// 生成软链，指向最新日志文件
	)
	writeMap :=lfshook.WriterMap{
		logrus.InfoLevel: logWriter,
		logrus.FatalLevel: logWriter,
		logrus.DebugLevel: logWriter,
		logrus.WarnLevel: logWriter,
		logrus.ErrorLevel: logWriter,
		logrus.PanicLevel: logWriter,
	}
	Hook := lfshook.NewHook(writeMap,&MyFormatter{})
	logger.AddHook(Hook)

	return logger
}

//自定义日志样式
type MyFormatter struct {}

func (m *MyFormatter) Format(entry *logrus.Entry) ([]byte, error) {
	var b *bytes.Buffer
	if entry.Buffer != nil{
		b = entry.Buffer
	}else {
		b = &bytes.Buffer{}
	}
	timestamp := entry.Time.Format("2006-01-02 15:04:05")
	var newLog string

	//HasCaller()为true才会有调用信息
	if entry.HasCaller(){
		fName := filepath.Base(entry.Caller.File)
		newLog = fmt.Sprintf("[%s] [%s] [%s:%d] %s\n",
			timestamp,entry.Level,fName,entry.Caller.Line,entry.Message)
	}else {
		newLog = fmt.Sprintf("[%s] [%s] %s\n", timestamp, entry.Level, entry.Message)
	}
	b.WriteString(newLog)
	return b.Bytes(), nil
}